﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using CMP.ERP.Model;
using CMP.ERP.BLL;

namespace CMP.ERP.Admin.Controllers
{
    public class ExperssController : Controller
    {
        //
        // GET: /Experss/
        //实例化对象
        SupplierBLL bll = new SupplierBLL();
        ProvinceBLL pbll = new ProvinceBLL();

        /// <summary>
        /// 显示供应商
        /// </summary>
        /// <returns></returns>
        public ActionResult Experss_Show()
        {
            //获取下拉框信息
            ViewBag.P_Id = new SelectList(pbll.GetProvince2(), "P_Id","P_Name");
            //获取客户管理信息
            List<Supplier> list = new List<Supplier>();
            DataTable data = bll.SupplierAll();
            foreach (DataRow item in data.Rows)
            {
                Supplier s = new Supplier();
                s.Supplier_Id = Convert.ToInt32(item["Supplier_Id"]);
                s.Supplier_Name = item["Supplier_Name"].ToString();
                s.Supplier_Address = item["Supplier_Address"].ToString();
                s.Supplier_Linkman = item["Supplier_Linkman"].ToString();
                s.Supplier_Phone = item["Supplier_Phone"].ToString();
                s.Supplier_Remark = item["Supplier_Remark"].ToString();
                s.P_Name = item["P_Name"].ToString();
                s.P_Id = Convert.ToInt32(item["P_Id"]);
                list.Add(s);
            }
            return View(list);
        }
        /// <summary>
        /// 删除供应商信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult DelSupplier_Info(int id)
        {
            if (bll.SupplierDelete(id) > 0)
            {
                return Content("<script>alert('删除成功！'); location.href='/Experss/Experss_Show'</script>");
            }
            else
            {
                return Content("<script>alert('删除失败！'); location.href='/Experss/Experss_Show'</script>");
            }
        }
        /// <summary>
        /// 省份下拉框
        /// </summary>
        /// <returns></returns>
        public ActionResult Custom_Info_Add()
        {
            ViewBag.P_Id = new SelectList(pbll.GetProvince2(), "P_Id", "P_Name");
            return View();
        }
        /// <summary>
        /// 添加供应商信息
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public ActionResult CustomInfo_Add(Supplier s)
        {
            if (bll.AddSupplier(s) > 0)
            {
                return Content("<script>alert('添加成功！'); location.href='/Experss/Experss_Show'</script>");
            }
            else
            {
                return Content("<script>alert('添加失败！'); location.href='/Experss/Experss_Show'</script>");
            }
        }
        /// <summary>
        /// 弹出供应商修改视图
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult EditSupplier(int id)
        {
            ViewBag.P_Id = new SelectList(pbll.GetProvince2(), "P_Id", "P_Name");
            DataTable dt = bll.GetSupplier(id);
            Supplier s2 = new Supplier();
            foreach (DataRow item in dt.Rows)
            {
                s2.Supplier_Id = Convert.ToInt32(item["Supplier_Id"]);
                s2.Supplier_Name = item["Supplier_Name"].ToString();
                s2.Supplier_Address = item["Supplier_Address"].ToString();
                s2.Supplier_Linkman = item["Supplier_Linkman"].ToString();
                s2.Supplier_Phone = item["Supplier_Phone"].ToString();
                s2.Supplier_Remark = item["Supplier_Remark"].ToString();
                s2.P_Id = Convert.ToInt32(item["P_Id"]);
            }
            return View(s2);
        }
        /// <summary>
        /// 实现供应商修改功能
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public ActionResult EditSupplier_Info(Supplier s)
        {
            if (bll.UptSupplier(s) > 0)
            {
                return Content("<script>alert('修改成功！'); location.href='/Experss/Experss_Show'</script>");
            }
            else
            {
                return Content("<script>alert('修改失败！'); location.href='/Experss/Experss_Show'</script>");
            }
        }
        /// <summary>
        /// 搜索(根据供应商名称进行查询)
        /// </summary>
        /// <param name="Supplier_Name"></param>
        /// <returns></returns>
        public ActionResult Supplier_Select(string Supplier_Name)
        {
            //获取下拉框信息
            ViewBag.P_Id = new SelectList(pbll.GetProvince2(), "P_Id", "P_Name");
            //获取客户管理信息
            List<Supplier> list = new List<Supplier>();
            DataTable data = bll.SearchSupplier(Supplier_Name);
            foreach (DataRow item in data.Rows)
            {
                Supplier s = new Supplier();
                s.Supplier_Id = Convert.ToInt32(item["Supplier_Id"]);
                s.Supplier_Name = item["Supplier_Name"].ToString();
                s.Supplier_Address = item["Supplier_Address"].ToString();
                s.Supplier_Linkman = item["Supplier_Linkman"].ToString();
                s.Supplier_Phone = item["Supplier_Phone"].ToString();
                s.Supplier_Remark = item["Supplier_Remark"].ToString();
                s.P_Id = Convert.ToInt32(item["P_Id"]);
                s.P_Name = item["P_Name"].ToString();
                list.Add(s);
            }
            return View("Experss_Show", list);
        }

    }
}
